package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.service.CatgoryService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CatgoryServiceImpl implements CatgoryService {
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Override
    public List<CatVo> selectlist() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("deleted",false);
        wrapper.eq("level","L1");
        List<CatVo> list1 = new ArrayList<>();

        List list = categoryMapper.selectList(wrapper);
        for(int i=0;i<list.size();i++){
            DtsCategory dtsCategory = (DtsCategory) list.get(i);

            CatVo catVo = new CatVo();
            catVo.setValue(dtsCategory.getId());
            catVo.setLabel(dtsCategory.getName());

            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.eq("pid",dtsCategory.getId());
            wrapper1.eq("level","L2");
            wrapper1.eq("deleted",false);
            List list2 = categoryMapper.selectList(wrapper1);
            List list4 = new ArrayList();
            for(int j=0;j<list2.size();j++){
                DtsCategory dtsCategory1= (DtsCategory) list2.get(j);

                CatVo catVo1 = new CatVo();
                catVo1.setValue(dtsCategory1.getId());
                catVo1.setLabel(dtsCategory1.getName());
                catVo1.setChildren(null);
                list4.add(catVo1);
            }
            catVo.setChildren(list4);


            list1.add(catVo);
        }
        return list1;
    }

    @Override
    public DtsCategory findById(Integer categoryId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",categoryId);

        DtsCategory dtsCategory = categoryMapper.selectById(categoryId);
        return dtsCategory;
    }

    @Override
    public Integer selectId(Integer categoryId) {
      QueryWrapper wrapper = new QueryWrapper();
      wrapper.eq("id",categoryId);

        DtsCategory dtsCategory = categoryMapper.selectById(categoryId);

        return dtsCategory.getPid();
    }






















}
